import Vue from 'vue'
import VueRouter from 'vue-router'

Vue.use(VueRouter)

const routes = [{
        path: '/login',
        component: () =>
            import ('@/views/Login'),
        name: 'login',

    },
    {
        path: '/home',
        component: () =>
            import ('@/views/Home'),
        name: 'home',
        redirect: '/users',
        children: [{
                path: '/users',
                name: 'users',
                component: () =>
                    import ('@/views/Users')
            }, {
                path: '/rights',
                name: 'rights',
                component: () =>
                    import ('@/views/Right/Right')
            }, {
                path: '/roles',
                name: 'roles',
                component: () =>
                    import ('@/views/Right/Role')
            }, {
                path: '/categories',
                name: 'categories',
                component: () =>
                    import ('@/views/Good/Categories')
            },
            {
                path: '/params',
                name: 'params',
                component: () =>
                    import ('@/views/Good/Params')
            },
            {
                path: '/goods',
                name: 'goods',
                component: () =>
                    import ('@/views/Good/Goods')
            },
            {
                path: '/goods/add',
                name: 'goods-add',
                component: () =>
                    import ('@/views/Good/Add')
            },
            {
                path: '/orders',
                name: 'orders',
                component: () =>
                    import ('@/views/Order/Order')
            },
            {
                path: '/reports',
                name: 'reports',
                component: () =>
                    import ('@/views/Reports')
            },
        ]

    },
    {
        path: '/',
        redirect: '/login'
    },

]

const router = new VueRouter({
    routes
})
router.beforeEach((to, from, next) => {
    if (['/', '/login'].includes(to.path)) {
        next()
    } else {
        if (sessionStorage.getItem('userinfo')) {
            next()
        } else {
            next('/login')
        }
    }
})

export default router